<?php

function dump($variable, $wrapper=true, $indent=0) {
    $result = '';
    if (is_object($variable)) {
        $indentFill = '';
        if ($indent > 0) {
            $indentFill = implode('', array_fill(0, $indent*2, ' '));
        }
        $rc = new ReflectionClass($variable);
        $result = "\n" . $indentFill . '<span class="object"><span class="type object">object</span>(' . $rc->getName() . ')' . "\n";
        $properties = $rc->getProperties();
        $items = array();
        foreach ($properties as $property) {
            $item = $indentFill . '  <span class="modifier">';
            if ($property->isPrivate()) {
                $item .= 'private';
            }
            if ($property->isProtected()) {
                $item .= 'protected';
            }
            if ($property->isPublic()) {
                $item .= 'public';
            }
            $item .= '</span>';
            $property->setAccessible(true);
            $item .= ' \'' . $property->getName() . '\' =&gt; ' . dump($property->getValue($variable), false, $indent + 2);
            $items[] = $item;
        }
        $result .= implode("\n", $items);
        $result .= '</span>';
    } else if ((string)$variable === $variable) {
        $result = '<span class="type">string</span> <span class="content">\'' . htmlspecialchars($variable) . '\'</span> <span class="length">(length=' . strlen($variable) . ')</span>';
    } else if ((int)$variable === $variable) {
        $result = '<span class="type">int</span> <span class="content">' . $variable . '</span>';
    } else if ((float)$variable === $variable) {
        $result = '<span class="type">float</span> <span class="content">' . $variable . '</span>';
    } else if ((bool)$variable === $variable) {
        $result = '<span class="type">boolean</span> <span class="content">' . ($variable === true ? 'true' : 'false') . '</span>';
    } else if (null === $variable) {
        $result = '<span class="content">null</span>';
    } else if ((array)$variable === $variable) {
        $indentFill = '';
        if ($indent > 0) {
            $indentFill = implode('', array_fill(0, $indent*2, ' '));
        }
        $result = "\n" . $indentFill . '<span class="array"><span class="type array">array</span><span class="length"> (items=' . count($variable) . ')</span><span class="items">' . "\n";
        $items = array();
        foreach ($variable as $key => $value) {
            $items[] = $indentFill . '  ' . (!is_numeric($key) ? '\'' : '') . $key . (!is_numeric($key) ? '\'' : '') . ' <span class="operator">=&gt;</span> ' . dump($value, false, $indent+2);
        }
        $result .= implode("\n", $items);
        $result .= '</span></span>';
    } else if (is_resource($variable)) {
        $result = '<span class="type">resource</span> <span class="content">' . ($variable === true ? 'true' : 'false') . '</span> <span class="type">(type=' . get_resource_type($variable) . ')</span>';
    }

    return ($wrapper === true ? '<pre class="vardump" dir="ltr">' : '') . $result . ($wrapper === true ? '</pre>' : '');
}